﻿using Gj;
using NaughtyAttributes;
using UnityEngine;

public class ListSize : NodeSize
{
    public ListHelper list;
    public GameObject parent;
    public float width;
    public float height;
    public Vector2 offset;

    protected override void OnRefresh(float rw, float rh, float w, float h, float s)
    {
        if (parent != null)
        {
            // TODO 会有延迟获取宽高会有异常
            var rect = parent.GetComponent<RectTransform>().rect;
            width = rect.width;
            height = rect.height;
            list.RefreshWidthHeight(rect.width, rect.height);
        }
        else
        {
            var cw = w / s;
            var ch = h / s;
            list.RefreshWidthHeight((width > 0 ? cw > width ? width : cw : cw) + offset.x,
                (height > 0 ? ch > height ? height : ch : ch) + offset.y);
        }
    }

    [Button]
    public void Refresh()
    {
        var rect = parent.GetComponent<RectTransform>().rect;
        width = rect.width;
        height = rect.height;
        list.RefreshWidthHeight(rect.width, rect.height);
    }
}